import loginWx from './login.js'
import api from 'utils/api.js'
module.exports = (params) => {
	let url = '';
	if(params.url.includes("http")){
		url = params.url;
	}else {
		url = api.ServerUrl + params.url;
	}
	let method = params.method;
	let header = params.header || {};
	let data = params.data || {};
	//	请求方式 GET POST
	if (method) {
		method = method.toUpperCase(); //	小写转大写
		if (method == "POST") {
			header = {
				"content-type": "application/x-www-form-urlencoded"
				
			}
		}
	}
	let token = uni.getStorageSync('token');
	if(!params.header) {
		header = {
			"Content-Type": "application/json;charset=UTF-8",
			"Authorization": "Bearer " + token,
		}
	}
	//	发起网络请求
	uni.request({
		url: url,
		method: method || "GET",
		header: header,
		data: data,
		dataType: "json",
		sslVerify: false, //	是否验证ssl证书
		success: res => {
			if (res.data.errcode == 40001) { //登录过期
				uni.removeStorageSync('token')
				uni.removeStorageSync('studentInfo')
				uni.removeStorageSync('token')
				uni.removeStorageSync('userid')
				loginWx(true)
				typeof params.success == "function" && params.success(res.data);
			} else {
				typeof params.success == "function" && params.success(res.data);
			}
		},
		fail: err => {
			uni.showModal({
				content: err.msg
			})
			typeof params.fail == "function" && params.fail(err.data);
		},
		complete: (e) => {
			// console.log(e,'url:', url,"请求完成");
			if (e.data && e.data.status == 500) { //接口报错500前端处理清除缓存重新请求
				uni.showToast({
					title:'网络请求错误',
					icon :'none'
				})
				loginWx(true)
			}
			typeof params.complete == "function" && params.complete(e.data);
			return;
		}
	})
}
